package com.team.databoard.controller;

import com.team.databoard.common.BaseResponse;
import com.team.databoard.common.ErrorCode;
import com.team.databoard.common.ResultUtils;
import com.team.databoard.exception.BusinessException;
import com.team.databoard.model.Student;
import com.team.databoard.model.request.StudentRequest;
import com.team.databoard.service.StudentService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.team.databoard.constant.UserConstant.USER_LOGIN_STATE;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @PostMapping("/add")
    public BaseResponse<Integer> addStudent(@RequestBody StudentRequest studentRequest, HttpServletRequest request) {
        // 校验登录状态
        if (request.getSession().getAttribute(USER_LOGIN_STATE) == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN);
        }
        // 校验
        if (studentRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Student student = new Student();
        student.setName(studentRequest.getName());
        student.setSex(studentRequest.getSex());
        student.setProvince(studentRequest.getProvince());
        student.setBirthday(studentRequest.getBirthday());
        student.setHeight(studentRequest.getHeight());
        student.setEducation(studentRequest.getEducation());

        //处理数据不全
        int result = studentService.insertStudent(student);
        if (result > 0) {
            return ResultUtils.success(result);
        }
        throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }

    @DeleteMapping("/{id}")
    public BaseResponse<Boolean> deleteStudent(@PathVariable(value = "id") int id, HttpServletRequest request) {
        // 校验登录状态
        if (request.getSession().getAttribute(USER_LOGIN_STATE) == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN);
        }
        // 校验
        if (id < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (studentService.deleteStudent(id)) {
            return ResultUtils.success(true);
        }
        throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateStudent(@RequestBody StudentRequest studentRequest, HttpServletRequest request) {
        // 校验登录状态
        if (request.getSession().getAttribute(USER_LOGIN_STATE) == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN);
        }
        // 校验
        if (studentRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Student student = new Student();
        student.setId(studentRequest.getId());
        student.setName(studentRequest.getName());
        student.setSex(studentRequest.getSex());
        student.setProvince(studentRequest.getProvince());
        student.setBirthday(studentRequest.getBirthday());
        student.setHeight(studentRequest.getHeight());
        student.setEducation(studentRequest.getEducation());
        if (studentService.updateStudent(student)) {
            return ResultUtils.success(true);
        }
        throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }

    @GetMapping("/{id}")
    public BaseResponse<Student> getStudentById(@PathVariable(value = "id") int id, HttpServletRequest request) {
        // 校验登录状态
        if (request.getSession().getAttribute(USER_LOGIN_STATE) == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN);
        }
        // 校验
        if (id < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Student student = studentService.selectStudent(id);
        if (student == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        return ResultUtils.success(student);
    }

    @GetMapping("/list")
    public BaseResponse<Map<String, Object>> getStudentList(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam Map<String, Object> params,
            HttpServletRequest request) {
        // 校验登录状态
        if (request.getSession().getAttribute(USER_LOGIN_STATE) == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN);
        }
        return ResultUtils.success(studentService.selectStudent(page, size, params));
    }
}
